# @Version        : 1.0
# @Update Time    : 2025/10/15 00:00
# @File           : auth_logic.py
# @IDE            : PyCharm
# @Desc           : 认证逻辑接口定义
from typing import Any, Literal, Optional, Sequence, Protocol, Self, Dict

from fastapi import Response

from authx.core.typings import LoginId
from authx.settings.config import CookieConfig


# 创建 AuthLogic  协议类


class AuthLogicProtocol(Protocol):
    """
    面向 StpLogic 的鸭子类型协议，抽象其公共接口以降低耦合。
    任何实现了这些方法的类，都可被 AuthManager/StpUtil 接纳。
    注：为避免包间强依赖，领域模型类型统一用 Any 表示；保持签名形状。
    """

    # 基本属性
    @property
    def login_type(self) -> str: ...

    # 设置登录类型
    def set_login_type(self, login_type: str) -> Self: ...

    # Token 读写
    def get_token_name(self) -> str: ...
    def create_token_value(
        self, login_id: Any, device_type: str, timeout: int, extra_data: Dict[str, Any]
    ) -> str: ...
    def set_token_value(
        self,
        response: Response,
        token_value: str,
        cookie_timeout: Optional[int] = None,
        login_parameter: Optional[Any] = None,
    ) -> None: ...
    def set_token_value_to_storage(self, token_value: str) -> None: ...
    def get_token_value(
        self, no_prefix_throw_exception: bool = False
    ) -> Optional[str]: ...
    def get_token_value_not_cut(self) -> Optional[str]: ...
    async def get_token_info(self) -> Any: ...

    def get_token_value_not_null(self) -> str: ...

    # 登录 / 会话创建
    async def login(
        self,
        *,
        response: Response,
        login_id: LoginId,
        device_type: Optional[str] = None,
        is_lasting_cookie: Optional[bool] = None,
        timeout: Optional[int] = None,
        login_parameter: Optional[Any] = None,
    ) -> None: ...
    def set_token_value_to_cookie(
        self,
        response: Response,
        token_value: str,
        cookie_config: Optional[CookieConfig] = None,
        cookie_timeout: Optional[int] = None,
    ): ...
    def set_token_value_to_response_header(
        self, response: Response, token_value: str
    ): ...
    def result_ok(self) -> Any: ...
    async def create_login_session(
        self, login_id: LoginId, login_parameter: Optional[Any] = None
    ) -> str: ...
    async def get_or_create_login_session(self, login_id: LoginId) -> str: ...

    # 注销 / 踢人 / 顶号
    async def logout(
        self,
        response: Response,
        *,
        login_id: Optional[LoginId] = None,
        device_type: Optional[str] = None,
        logout_parameter: Optional[Any] = None,
    ) -> None: ...
    async def logout_by_token_value(
        self, token_value: str, logout_parameter: Optional[Any] = None
    ) -> None: ...
    async def kickout(
        self,
        login_id: LoginId,
        device_type: Optional[str] = None,
        logout_parameter: Optional[Any] = None,
    ) -> None: ...
    async def kickout_by_token_value(
        self, token_value: str, logout_parameter: Optional[Any] = None
    ) -> None: ...
    async def replaced_by_token_value(
        self, token_value: str, logout_parameter: Optional[Any] = None
    ) -> None: ...
    def remove_terminal_by_kickout(self, session: Any, terminal: Any) -> None: ...
    def remove_terminal_by_replaced(self, session: Any, terminal: Any) -> None: ...
    def remove_terminal_by_logout(self, session: Any, terminal: Any) -> None: ...

    # 会话与登录态查询
    async def is_login(self, login_id: Optional[LoginId] = None) -> bool: ...
    async def check_login(self) -> None: ...
    async def get_login_id(self) -> LoginId: ...
    async def get_login_id_default_null(self) -> Optional[LoginId]: ...
    async def get_login_id_by_token(self, token_value: str) -> Optional[LoginId]: ...
    async def get_login_id_by_token_not_think_freeze(
        self, token_value: str
    ) -> Optional[LoginId]: ...

    # Session 相关
    async def get_session_by_login_id(
        self, login_id: LoginId, is_create: bool = True
    ) -> Any: ...
    async def get_session_by_session_id(self, session_id: str) -> Optional[Any]: ...
    async def get_session(self, is_create: Optional[bool] = None) -> Any: ...
    async def get_token_session_by_token(self, token_value: str) -> Any: ...
    async def get_token_session(self) -> Any: ...
    async def get_anon_token_session(self) -> Any: ...

    # 活跃度与过期时间
    async def update_last_active_to_now(self) -> None: ...
    async def check_active_timeout(self) -> None: ...
    async def get_token_last_active_time(self) -> int: ...
    async def get_token_timeout(self, token: Optional[str] = None) -> int: ...
    async def get_session_timeout(self) -> int: ...
    async def get_token_session_timeout(self) -> int: ...
    async def get_token_active_timeout(self) -> int: ...
    async def renew_timeout(
        self, timeout: int, token_value: Optional[str] = None
    ) -> None: ...

    # 角色与权限
    async def get_role_list(
        self, login_id: Optional[LoginId] = None
    ) -> Sequence[str]: ...
    async def has_single_role(
        self, role: str, login_id: Optional[LoginId] = None
    ) -> bool: ...
    async def has_role(
        self, *role: str, logic: Literal["and", "or"] = "and"
    ) -> bool: ...
    async def check_role(
        self, *role: str, logic: Literal["and", "or"] = "and"
    ) -> None: ...
    async def get_permission_list(
        self, login_id: Optional[LoginId] = None
    ) -> Sequence[str]: ...
    async def has_single_permission(
        self, permission: str, login_id: Optional[LoginId] = None
    ) -> bool: ...
    async def has_permission(
        self, *permission: str, logic: Literal["and", "or"] = "and"
    ) -> bool: ...
    async def check_permission(
        self, *permission: str, logic: Literal["and", "or"] = "and"
    ) -> None: ...
    async def is_super_admin(self, login_id: Optional[LoginId] = None) -> bool: ...

    # Token 反查 & 终端信息
    async def get_token_value_by_login_id(
        self, login_id: LoginId, device_type: Optional[str] = None
    ) -> Optional[str]: ...
    async def get_token_value_list_by_login_id(
        self, login_id: LoginId, device_type: Optional[str] = None
    ) -> Sequence[str]: ...
    async def get_terminal_list_by_login_id(
        self, login_id: LoginId, device_type: Optional[str] = None
    ) -> Sequence[Any]: ...
    async def get_terminal_info(self) -> Optional[Any]: ...
    async def get_terminal_info_by_token(self, token_value: str) -> Optional[Any]: ...
    async def get_login_device_type(self) -> Optional[str]: ...
    async def get_login_device_type_by_token(
        self, token_value: str
    ) -> Optional[str]: ...
    async def get_login_device_id(self) -> Optional[str]: ...
    async def get_login_device_id_by_token(self, token_value: str) -> Optional[str]: ...
    async def is_trust_device_id(self, login_id: LoginId, device_id: str) -> bool: ...

    # 会话管理搜索
    async def search_token_value(
        self, keyword: str, start: int = 0, size: int = 10, sort_type: bool = True
    ) -> Sequence[str]: ...
    async def search_session_id(
        self,
        keyword: Optional[str],
        start: int = 0,
        size: int = 10,
        sort_type: bool = True,
    ) -> Sequence[str]: ...
    async def search_token_session_id(
        self,
        keyword: Optional[str],
        start: int = 0,
        size: int = 10,
        sort_type: bool = True,
    ) -> Sequence[str]: ...

    # 账号封禁
    async def disable(
        self, login_id: LoginId, time: int, server: Optional[str] = None
    ) -> None: ...
    async def is_disable(self, login_id: LoginId) -> bool: ...
    async def check_disable(
        self, login_id: LoginId, service: Optional[str] = None
    ) -> None: ...
    async def get_disable_time(self, login_id: LoginId) -> int: ...
    async def untie_disable(self, login_id: LoginId) -> None: ...

    # 参数构造（可选）
    def create_login_parameter(self) -> Any: ...
    def create_sa_login_parameter(self) -> Any: ...
    def create_sa_logout_parameter(self) -> Any: ...
